package cn.xiaolang.function.file_manage;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 深入调研文件IO
 *
 * @author Dean
 * @date 2022/4/4
 */
public class ByteBufferDemo {

    public static void main(String[] args) throws Exception {
        writeByteBuffer();
    }

    private static void writeByteBuffer() {
        ByteBuffer buffer = ByteBuffer.allocateDirect(8192);
        System.out.println("init,mark:" + buffer);
        buffer.put("123".getBytes());
        System.out.println("put-123, mark:" + buffer);
        // 读写翻转
        buffer.flip();
        System.out.println("flip, mark:" + buffer);

        buffer.get();
        System.out.println("get, mark:" + buffer);

        //挤压空间，继续存储
        buffer.compact();
        System.out.println("compact, mark:" + buffer);
        //
        buffer.put("456".getBytes());
        System.out.println("put-456, mark:" + buffer);

    }

}
